<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sobjectproduce
 *
 * @author Dragon.hu add @2015.9.22
 */
defined('BASEPATH') or die('forbidden');

class Equip_Controller extends Controller {

	private $inc;
	private $logdb;	//数据库连接
	private $id2name;
	
	function _init() {
		check_level(50, segment(0), segment(1), segment(2, 'index'));
		set_time_limit(0);
		
		$this->inc = $this->config['inc'];

		
        // log数据库
		//得到选择的区号
		$areanum = $_SESSION['area_selected_id'];
		if ($areanum == '')
		{
			echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
			return;
		}
		// log数据库
        $this->logdb = $this->load->database('log_db');
        //$this->logdb = getTmpDbConnect(PLATFORM_ALL, 'all', $areanum, DBTYPE_LOG);
		if (false == $this->logdb) {
			//内部有提示错误，在此就不提示了
			 //echo "<script>alert(' connect areanum:$areanum error ');</script>";
            return;
		}

       $this->id2name = array();
        $copysql =  "select id, name from tt_equipid2name";
        $this->db->query ( $copysql );
        while ( $n = $this->db->get_array () ) {
            $this->id2name[(int)$n ['id']] = $n ['name'];
        }
	}

	function index() {
		$this->view->display("log_object/equip/index");
	}

	function flexigrid() {
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";

        $areanum = $_SESSION['area_selected_id'];
        if ($where == ""){
            $where = "svrid=" . $areanum;
        }else{
            $where = "svrid=" . $areanum . " and (" . $where . ")"; 
        }

        $mapAreaNum = ((int)$areanum)%5+1;

        //$table_name = "gamelog_7_".$mapAreaNum;
		$table_name = "gamelog_7";

        $sql = "SELECT COUNT(*) AS num FROM {$table_name} where {$where} ";
        $one = $this->logdb->one($sql);
        $total = $one['num'];

        $sql1 = "SELECT {$colsinfo} FROM {$table_name} where {$where}  {$orderby} LIMIT {$offset},{$rp}";
        $this->logdb->query($sql1);
        $result1=$this->logdb->row('default');
        //$total=count($result1);

        foreach ($result1 as $k=>$n){
            $n['time']=date('Y/m/d H:i:s',$n['time']);
            $n['eventid']=$this->config['event'][$n['eventid']];
            $n['srcid']=$this->config['source'][$n['srcid']];
            $n['type']=$this->config['chtype'][$n['type']];
            
            if (0 == (int)$n['param1']){ //多个装备用extra保存id列表
                $equpis=json_decode($n['extra'],true);
                $equips_str = "";
                foreach ($equpis['Items'] as $key => $value) {
                    $equipname = $this->id2name[(int)$value['ID']];
                    $equips_str = $equips_str . $equipname . "*" . $value['Num'] . "  " ;
                }
                $n['param1']=$equips_str;
            }else{
                $n['param1']=$this->id2name[(int)$n['param1']];
            }

            $n['extra']=($n['param2']>>32) & 0x00000000ffffffff;
            $n['param2'] = $n['param2'] & 0x00000000ffffffff;
            $rows[] = array('id' => $n['id'], 'cell' => $n);
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );

        echo json_encode($result);
	}

}
